package lily.phs.healthedu;

import java.security.PublicKey;
import javax.security.auth.PrivateCredentialPermission;
import lily.phs.object.HealthEdu;
import lily.phs.object.HereTipa;
import lily.phs.object.VisitMentals;
import lily.phs.urh.R;
import lily.ui.control.MyPopupWindow;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class HealthEduView extends LinearLayout implements OnFocusChangeListener,OnClickListener
{
	private MyPopupWindow mPopupWindow;
	private Context mContext;
	private EditText et_time,et_place,et_form,et_theme,et_organizer,et_sort,et_acc_num,et_kind_num,
					 et_content,et_evaluate,et_person_sign,et_charge_sign,et_filltime;
	private CheckBox cb_write,cb_picture,cb_print,cb_audio,cb_assign,cb_other;
	private HealthEdu he;
	private HereTipa ht;
	private int fileid = 0;
	private TextView tv;
	private LinearLayout llContent;
	private View child;
	private LinearLayout ll;
	
	public HealthEduView(Context context,int fileid)
	
	{
		super(context);
		this.mContext=context;
		this.fileid = fileid;
		
		final LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		child = inflater.inflate(R.layout.layout_x1, null);

	}
		

	
//	class MyView extends View
//	{
//		Paint paint;
//		public MyView(Context context)
//		{
//			super(context);
//			paint=new Paint();
//			paint.setStyle(Paint.Style.STROKE);
//			paint.setColor(Color.BLACK);
//		}
//		@Override
//		public void onDraw(Canvas c)
//		{
//			System.out.println("ok");
//			int y=0;
//			c.drawLine(15, 80-y, 783, 80-y, paint);
//			c.drawLine(15, 112-y, 783, 112-y, paint);
//			c.drawLine(15, 144-y, 783, 144-y, paint);
//			c.drawLine(15, 176-y, 783, 176-y, paint);
//			c.drawLine(15, 208-y, 783, 208-y, paint);
//			c.drawLine(15, 240-y, 783, 240-y, paint);
//			c.drawLine(15, 272-y, 783, 272-y, paint);
//			c.drawLine(15, 460-y, 783, 460-y, paint);
//			c.drawLine(15, 610-y, 783, 610-y, paint);
//			c.drawLine(15, 750-y, 783, 750-y, paint);
//			c.drawLine(15, 80-y, 15, 750-y, paint);
//			c.drawLine(783, 80-y, 783, 751-y, paint);
//			c.drawLine(395, 80-y, 395, 112-y, paint);
//			c.drawLine(395, 208-y, 395, 240-y, paint);
//			System.out.println("ok-1");
//		}
//	}
	
		public void findViewsById()
		{
			et_time = (EditText)child.findViewById(R.id.activitytime);
			et_time.setInputType(InputType.TYPE_NULL);//自己加的
			et_time.setOnClickListener(this);
			et_time.setOnFocusChangeListener(this);
			et_place=(EditText)child.findViewById(R.id.activitylocation);
			et_form=(EditText)child.findViewById(R.id.activityformxs);
			et_theme=(EditText)child.findViewById(R.id.activityform);
			et_organizer=(EditText)child.findViewById(R.id.organizer);
			et_sort=(EditText)child.findViewById(R.id.acceptedu);
			et_acc_num=(EditText)child.findViewById(R.id.numaccedu);
			et_kind_num=(EditText)child.findViewById(R.id.kindnumedumat);
			et_content=(EditText)child.findViewById(R.id.activitycontent);
			et_evaluate=(EditText)child.findViewById(R.id.activityevaluation);
			et_person_sign=(EditText)child.findViewById(R.id.personalsignature);
			et_charge_sign=(EditText)child.findViewById(R.id.authorizedsignature);
			et_filltime=(EditText)child.findViewById(R.id.filltime);
			et_filltime.setInputType(InputType.TYPE_NULL);//自己加的
			et_filltime.setOnClickListener(this);
			et_filltime.setOnFocusChangeListener(this);
			
			cb_write=(CheckBox)child.findViewById(R.id.writtenmaterial);
			cb_picture=(CheckBox)child.findViewById(R.id.picturematerial);
			cb_print=(CheckBox)child.findViewById(R.id.printmaterial);
			cb_audio=(CheckBox)child.findViewById(R.id.audiomaterial);
			cb_assign=(CheckBox)child.findViewById(R.id.attendance);
			cb_other=(CheckBox)child.findViewById(R.id.othermaterial);
		}
		
		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			if(v==et_time&&hasFocus){
				mPopupWindow = new MyPopupWindow(mContext);			
				mPopupWindow.showAtLocation(v.findViewById(R.id.activitytime), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_time.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
			}
			else if(v==et_filltime&&hasFocus){
				mPopupWindow = new MyPopupWindow(mContext);			
				mPopupWindow.showAtLocation(v.findViewById(R.id.filltime), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_filltime.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
			}
		}
	
		public void initViews()
		{
			initialState();
			String temp="";
			int itemp = -1;
			//temp = ht.getHeti_name();
			
			//temp = ht.getHeti_here_id();
			
			temp = he.getHere_theme();
			if(temp.length()>0){
				et_theme.setText(temp);
			}
			temp = he.getHere_charge_sign();
			if(temp.length()>0){
				et_charge_sign.setText(temp);
			}
			temp = he.getHere_content();
			if(temp.length()>0){
				et_content.setText(temp);
			}
			temp = he.getHere_evaluate();
			if(temp.length()>0){
				et_evaluate.setText(temp);
			}
			temp = he.getHere_filltime();
			if(temp.length()>0){
				et_filltime.setText(temp);
			}
			temp = he.getHere_form();
			if(temp.length()>0){
				et_form.setText(temp);
			}
			temp = he.getHere_kind_num();
			if(temp.length()>0){
				et_kind_num.setText(temp);
			}
			temp = he.getHere_organizer();
			if(temp.length()>0){
				et_organizer.setText(temp);
			}
			temp = he.getHere_person_sign();
			if(temp.length()>0){
				et_person_sign.setText(temp);
			}
			temp = he.getHere_place();
			if(temp.length()>0){
				et_place.setText(temp);
			}
			temp = he.getHere_sort();
			if(temp.length()>0){
				et_sort.setText(temp);
			}
			temp = he.getHere_time();
			if(temp.length()>0){
				et_time.setText(temp);
			}
			
			
			itemp = he.getHere_acc_num();
			if(itemp>=0){
				et_acc_num.setText(String.valueOf(itemp));
			}
			
			
			temp=he.getHere_material();
			if(temp.length()>0)
			{
				String[] ss = temp.split(",");
				for(int i=0;i<ss.length;i++)
				{
					int k = Integer.parseInt(ss[i]);
					switch(k)
						{
						case 1:
						{
							cb_write.setChecked(true);
							break;
						}
						case 2:
						{
							cb_picture.setChecked(true);
							break;
						}
						case 3:
						{
							cb_print.setChecked(true);
							break;
						}
						case 4:
						{
							cb_audio.setChecked(true);
							break;
						}
						case 5:
						{
							cb_assign.setChecked(true);
							break;
						}
						case 6:
						{
							cb_other.setChecked(true);
							break;
						}
						}
				}
			}
		}

		private void initialState() 
		{
			// TODO Auto-generated method stub
			et_time.setText("");
			et_place.setText("");
			et_form.setText("");
			et_theme.setText("");
			et_organizer.setText("");
			et_sort.setText("");
			et_acc_num.setText("");
			et_kind_num.setText("");
			et_content.setText("");
			et_evaluate.setText("");
			et_person_sign.setText("");
			et_charge_sign.setText("");
			et_filltime.setText("");
			
			cb_write.setChecked(false);
			cb_picture.setChecked(false);
			cb_print.setChecked(false);
			cb_audio.setChecked(false);
			cb_assign.setChecked(false);
			cb_other.setChecked(false);
			
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(v==et_time){
				mPopupWindow = new MyPopupWindow(mContext);			
				mPopupWindow.showAtLocation(v.findViewById(R.id.activitytime), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_time.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
			}
			else if(v==et_filltime){
				mPopupWindow = new MyPopupWindow(mContext);			
				mPopupWindow.showAtLocation(v.findViewById(R.id.filltime), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_filltime.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
			}
		}	
		
		public void fetchData(){
			//ht = new HereTipa(mContext);
			//ht.selectNameAndCode(_ID);
			
			he = new HealthEdu(mContext);
			he.set_Id(fileid);
			he.selectHealthEdu(fileid);
		}
		
		/**
		 * 将view添加到父view中
		 */
		public void setMyView(){
			this.removeAllViews();
			LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
			this.addView(child,lp);
//			MyView lines = new MyView(mContext);
//			this.addView(lines, lp);
		}
		
		/**
		 * 将整个界面作为一个view对象
		 * @return 整个界面view
		 */
		public View getMyView(){
			return child;
		}
		
		
		public void saveViews()
		{
			HealthEdu he1 = new HealthEdu(mContext);
			//he.selectHealthEdu(profileId, times);//应该有这句话吗，我的没有profileId
			he1.set_Id(fileid);
			he1.selectHealthEdu(fileid);
			
			String temp = "";
			StringBuffer sb;
			
			temp = et_theme.getText().toString();
			if(temp.length()>0){
				he1.setHere_theme(temp);
			}
			temp = et_time.getText().toString();
			if(temp.length()>0){
				he1.setHere_time(temp);
			}
			temp = et_place.getText().toString();
			if(temp.length()>0){
				he1.setHere_place(temp);
			}
			temp = et_form.getText().toString();
			if(temp.length()>0){
				he1.setHere_form(temp);
			}
			temp = et_organizer.getText().toString();
			if(temp.length()>0){
				he1.setHere_organizer(temp);
			}
			temp = et_sort.getText().toString();
			if(temp.length()>0){
				he1.setHere_sort(temp);
			}
			temp = et_kind_num.getText().toString();
			if(temp.length()>0){
				he1.setHere_kind_num(temp);
			}
			temp = et_content.getText().toString();
			if(temp.length()>0){
				he1.setHere_content(temp);
			}
			temp = et_evaluate.getText().toString();
			if(temp.length()>0){
				he1.setHere_evaluate(temp);
			}
			temp = et_person_sign.getText().toString();
			if(temp.length()>0){
				he1.setHere_person_sign(temp);
			}
			temp = et_charge_sign.getText().toString();
			if(temp.length()>0){
				he1.setHere_charge_sign(temp);
			}
			temp = et_filltime.getText().toString();
			if(temp.length()>0){
				he1.setHere_filltime(temp);
			}
			
			temp = et_acc_num.getText().toString();
			if(temp.length()>0){
				he1.setHere_acc_num(Integer.parseInt(temp));
			}

			sb = new StringBuffer();
			if(cb_write.isChecked()==true)
			{
				sb.append("1,");
			}			
			if(cb_picture.isChecked()==true)
			{
				sb.append("2,");
			}
			if(cb_print.isChecked()==true)
			{
				sb.append("3,");
			}
			if(cb_audio.isChecked()==true)
			{
				sb.append("4,");
			}
			if(cb_assign.isChecked()==true)
			{
				sb.append("5,");
			}
			if(cb_other.isChecked()==true)
			{
				sb.append("6,");
			}
			he1.setHere_material(sb.toString());
			
			
			he1.updateHealthEdu(fileid); //我这个档案没编号，怎么办？？？
		}
}
